/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.work;

import java.awt.Component;
import javax.swing.JTextField;

/**
 *
 * @author JSJSYS
 */
public class App extends javax.swing.JFrame {

    private boolean nowshow = true;
    private double nowvalue = 0;
    private double passvalue = 0;
    private String operator = "";
    /**
     * Creates new form App
     */
    public App() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lltext = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        llButton_init = new javax.swing.JButton();
        llButton_add = new javax.swing.JButton();
        llButton_subtract = new javax.swing.JButton();
        llButton_multiply = new javax.swing.JButton();
        llButton7 = new javax.swing.JButton();
        llButton8 = new javax.swing.JButton();
        llButton9 = new javax.swing.JButton();
        llButton_divide = new javax.swing.JButton();
        llButton4 = new javax.swing.JButton();
        llButton5 = new javax.swing.JButton();
        llButton6 = new javax.swing.JButton();
        llButton_equal = new javax.swing.JButton();
        llButton1 = new javax.swing.JButton();
        llButton2 = new javax.swing.JButton();
        llButton3 = new javax.swing.JButton();
        llButton0 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("计算器"));

        lltext.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        lltext.setText("0");
        lltext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lltextActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lltext)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lltext, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setLayout(new java.awt.GridLayout(4, 4));

        llButton_init.setText("AC");
        llButton_init.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton_initActionPerformed(evt);
            }
        });
        jPanel2.add(llButton_init);

        llButton_add.setText("＋");
        llButton_add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton_addActionPerformed(evt);
            }
        });
        jPanel2.add(llButton_add);

        llButton_subtract.setText("－");
        llButton_subtract.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton_subtractActionPerformed(evt);
            }
        });
        jPanel2.add(llButton_subtract);

        llButton_multiply.setText("×");
        llButton_multiply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton_multiplyActionPerformed(evt);
            }
        });
        jPanel2.add(llButton_multiply);

        llButton7.setText("7");
        llButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton7ActionPerformed(evt);
            }
        });
        jPanel2.add(llButton7);

        llButton8.setText("8");
        llButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton8ActionPerformed(evt);
            }
        });
        jPanel2.add(llButton8);

        llButton9.setText("9");
        llButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton9ActionPerformed(evt);
            }
        });
        jPanel2.add(llButton9);

        llButton_divide.setText("÷");
        llButton_divide.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton_divideActionPerformed(evt);
            }
        });
        jPanel2.add(llButton_divide);

        llButton4.setText("4");
        llButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton4ActionPerformed(evt);
            }
        });
        jPanel2.add(llButton4);

        llButton5.setText("5");
        llButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton5ActionPerformed(evt);
            }
        });
        jPanel2.add(llButton5);

        llButton6.setText("6");
        llButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton6ActionPerformed(evt);
            }
        });
        jPanel2.add(llButton6);

        llButton_equal.setText("=");
        llButton_equal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton_equalActionPerformed(evt);
            }
        });
        jPanel2.add(llButton_equal);

        llButton1.setText("1");
        llButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(llButton1);

        llButton2.setText("2");
        llButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(llButton2);

        llButton3.setText("3");
        llButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton3ActionPerformed(evt);
            }
        });
        jPanel2.add(llButton3);

        llButton0.setText("0");
        llButton0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                llButton0ActionPerformed(evt);
            }
        });
        jPanel2.add(llButton0);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void llButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton7ActionPerformed
        // TODO add your handling code here:
        showtext("7");
    }//GEN-LAST:event_llButton7ActionPerformed
    
    private void lltextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lltextActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_lltextActionPerformed

    private void llButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton8ActionPerformed
        // TODO add your handling code here:
        showtext("8"); 
    }//GEN-LAST:event_llButton8ActionPerformed

    private void llButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton9ActionPerformed
        // TODO add your handling code here:
        showtext("9"); 
    }//GEN-LAST:event_llButton9ActionPerformed

    private void llButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton4ActionPerformed
        // TODO add your handling code here:
        showtext("4"); 
    }//GEN-LAST:event_llButton4ActionPerformed

    private void llButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton5ActionPerformed
        // TODO add your handling code here:
        showtext("5"); 
    }//GEN-LAST:event_llButton5ActionPerformed

    private void llButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton6ActionPerformed
        // TODO add your handling code here:
        showtext("6"); 
    }//GEN-LAST:event_llButton6ActionPerformed

    private void llButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton1ActionPerformed
        // TODO add your handling code here:
        showtext("1"); 
    }//GEN-LAST:event_llButton1ActionPerformed

    private void llButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton2ActionPerformed
        // TODO add your handling code here:
        showtext("2"); 
    }//GEN-LAST:event_llButton2ActionPerformed

    private void llButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton3ActionPerformed
        // TODO add your handling code here:
        showtext("3"); 
    }//GEN-LAST:event_llButton3ActionPerformed

    private void llButton0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton0ActionPerformed
        // TODO add your handling code here:
        showtext("0"); 
    }//GEN-LAST:event_llButton0ActionPerformed

    private void llButton_initActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton_initActionPerformed
        // TODO add your handling code here:
        lltext.setText("0");
        passvalue = 0;
        nowvalue = 0;
        nowshow = true;
        operator = "";
    }//GEN-LAST:event_llButton_initActionPerformed

    private void llButton_addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton_addActionPerformed
        // TODO add your handling code here:
        initOperator("add");
    }//GEN-LAST:event_llButton_addActionPerformed

    private void llButton_subtractActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton_subtractActionPerformed
        // TODO add your handling code here:
        initOperator("subtract");
    }//GEN-LAST:event_llButton_subtractActionPerformed

    private void llButton_multiplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton_multiplyActionPerformed
        // TODO add your handling code here:
        initOperator("multiply");
    }//GEN-LAST:event_llButton_multiplyActionPerformed

    private void llButton_divideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton_divideActionPerformed
        // TODO add your handling code here:
        initOperator("divide");
    }//GEN-LAST:event_llButton_divideActionPerformed

    private void llButton_equalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_llButton_equalActionPerformed
        // TODO add your handling code here:
        switch (operator){
            case "add":nowvalue = passvalue + nowvalue;break;
            case "subtract":nowvalue = passvalue - nowvalue;break;
            case "multiply":nowvalue = passvalue * nowvalue;break;
            case "divide":nowvalue = passvalue / nowvalue;break;
        }
        lltext.setText(nowvalue+"");
        nowshow = true;
        operator = "";
    }//GEN-LAST:event_llButton_equalActionPerformed

    void showtext(String str){
        String nowtext = lltext.getText();
        if(nowtext.equals("0") || nowshow){
            lltext.setText(str);            
        }
        else{
            lltext.setText(nowtext + str);
        }
        String show =  lltext.getText();
        nowvalue = Double.parseDouble(show);        
        nowshow = false;
    }
    
    void initOperator(String str){
        passvalue = nowvalue;
        nowshow = true;
        operator = str;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new App().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton llButton0;
    private javax.swing.JButton llButton1;
    private javax.swing.JButton llButton2;
    private javax.swing.JButton llButton3;
    private javax.swing.JButton llButton4;
    private javax.swing.JButton llButton5;
    private javax.swing.JButton llButton6;
    private javax.swing.JButton llButton7;
    private javax.swing.JButton llButton8;
    private javax.swing.JButton llButton9;
    private javax.swing.JButton llButton_add;
    private javax.swing.JButton llButton_divide;
    private javax.swing.JButton llButton_equal;
    private javax.swing.JButton llButton_init;
    private javax.swing.JButton llButton_multiply;
    private javax.swing.JButton llButton_subtract;
    private javax.swing.JTextField lltext;
    // End of variables declaration//GEN-END:variables
}
